﻿using System;
using System.Text;
using System.Web;
using System.Data;
using System.Linq;
using System.Data.SqlClient;
using System.Reflection;
using System.Collections.Generic;

using VIT.DataHelper.DataAccess;
using VIT.Library;

namespace VIT.Web.Modules.Products
{
    public class ProductProvider
    {
        SQLSupport sqlSupport = new SQLSupport();

        #region Properties
        public int Id { get; set; }
        public int CategoryId { get; set; }
        public int ManufacturerId { get; set; }
        public int SupplierId { get; set; }
        public string LanguageId { get; set; }
        public string Type { get; set; }
        public string Image { get; set; }
        public string Title { get; set; }
        public string Brief { get; set; }
        public string Description { get; set; }
        public string Tag { get; set; }
        public decimal Price { get; set; }
        public decimal Sale { get; set; }
        public int Views { get; set; }
        public int Orders { get; set; }
        public bool Published { get; set; }
        #endregion

        #region Constructor
        public ProductProvider(int id = -1,
                        int categoryId = 0,
                        int manufacturerId = 0,
                        int supplierId = 0,
                        decimal price = 0,
                        decimal sale = 0,
                        int views = 0,
                        string languageId = "",
                        string image = "",
                        string title = "", 
                        string brief = "",
                        string description = "",
                        string tag = "",
                        string type="",
                        int orders = 0,
                        bool published = true)
        {
            this.Id = id;
            this.CategoryId = categoryId;
            this.ManufacturerId = manufacturerId;
            this.SupplierId = supplierId;
            this.LanguageId = languageId;
            this.Type = type;
            this.Image = image;
            this.Title = title;
            this.Brief = brief;
            this.Description = description;
            this.Price = price;
            this.Sale = sale;
            this.Views = views;
            this.Tag = tag;
            this.Orders = orders;
            this.Published = published;
        }
        #endregion

        #region Pre
        public DataTable GetAllCategory(string categorylist = "")
        {
            string strQuery = string.Empty;
            if (categorylist == "")
                strQuery = "select Id, Title, ParentId, Image, Description, Orders, Published from Category c left join Category_Language l on Id=CategoryId and LanguageID=@LanguageID where Deleted=0";
            else
                strQuery = "select Id, Title, ParentId, Image, Description, Orders, Published from Category c left join Category_Language l on Id=CategoryId and LanguageID=@LanguageID where Deleted=0 and Id in (" + categorylist + ")";
            SqlParameter LanguageId = new SqlParameter("@LanguageID", this.LanguageId);
            return sqlSupport.ExecuteDataset(false, strQuery, LanguageId).Tables[0];
        }

        public DataTable getTopViewProduct(Dictionary<string, string> param, ref string Category)
        {
            int Top = 0;
            if (param.ContainsKey("Top")) int.TryParse(param["Top"], out Top);

            string Type;
            if (param.ContainsKey("Type")) Type = param["Type"];
            else Type = "";

            int Color = 0;
            if (param.ContainsKey("Color"))
                int.TryParse(param["Color"], out Color);

            int Style = 0;
            if (param.ContainsKey("Style"))
                int.TryParse(param["Style"], out Style);

            string listCategory = string.Empty;
            if (param["Category"] != null)
            {
                Category = param["Category"];

                bool InChildCat = false;
                try
                {
                    InChildCat = param["InChildCat"].ToUpper() == "true".ToUpper() ? true : false;
                    if (InChildCat)
                    {
                        DataTable table = GetAllCategory();
                        listCategory = table.GetChildId(Category);
                    }
                    else
                    {
                        listCategory = Category;
                    }
                }
                catch
                {
                    InChildCat = false;
                    listCategory = Category;
                }
                
            }
            else Category = "";

            bool HasFavourite = false;
            try
            {
                HasFavourite = param["HasFavourite"].ToUpper() == "true".ToUpper() ? true : false;
            }
            catch
            {
                HasFavourite = false;
            }

            string ProductNotIn = string.Empty;
            if (param.ContainsKey("ProductNotIn")) ProductNotIn = param["ProductNotIn"];
            else ProductNotIn = "";

            return getListProduct(Top, Type, Color, Style, listCategory, HasFavourite, ProductNotIn);
        }
        private DataTable getListProduct(int top = 0, string type="", int Color=0, int Style=0, string catList="", bool fav=false, string prNotIn = "")
        {
            StringBuilder strQuery = new StringBuilder();
            strQuery.Append("select ");
            if (top != 0)
            {
                strQuery.Append("top ");
                strQuery.Append(top);
            }

            strQuery.Append(" Products.id, Products_Language.Title, Category_Language.Title As CategoryName, Products.[Image], Products_Language.Brief, Products_Language.[Description],Products.Color,Products.Style, Products.Price, Products.Sale, Products.Tag, [Views] FROM Products, Products_Language, Category_Language where Products.Id=ProductId and Products_Language.LanguageId=@LanguageId and Category_Language.CategoryId=Products.CategoryId and Published=1 ");
            strQuery.Append(type.Length>0? "and Type like @Type ":" ");
            strQuery.Append(catList.Length > 0 ? "and Products.CategoryId in (" + catList + ") " : " ");
            strQuery.Append(prNotIn.Length > 0 ? "and Products.Id not in (" + prNotIn + ") " : " ");
            strQuery.Append(fav ? " order by Views/(DATEDIFF(day,GETDATE()+1,Products_Language.ModifyDate))" : " order by Orders");

            SqlParameter Type = new SqlParameter("@Type", "%" + type.Replace(" ", "%") + "%");
            SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
            DataTable table = sqlSupport.ExecuteDataset(false, strQuery.ToString(), LanguageId, Type).Tables[0];

            int n = table.Rows.Count;
            if(n>0)
            {
                for (int i = n - 1; i > -1;i-- )
                {
                    if (Color > 0)
                    {
                        if (table.Rows[i]["Color"].ToString().Split(',').Contains(Color.ToString()))
                            table.Rows.RemoveAt(i);
                    }
                    else if (Style > 0)
                    {
                        if (table.Rows[i]["Style"].ToString().Split(',').Contains(Color.ToString()))
                            table.Rows.RemoveAt(i);

                    }
                }
            }
            return table;
        }
        #endregion
    }
}
